package cn.com.cifi.mars.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.cifi.mars.bean.dto.AllProjectIndexDto;
import cn.com.cifi.mars.bean.dto.ProjectCountDto;
import cn.com.cifi.mars.bean.dto.ProjectStagesDto;
import cn.com.cifi.mars.bean.dto.ProjectStagesVersionDto;
import cn.com.cifi.mars.bean.dto.ProjectUnderwayDto;
import cn.com.cifi.mars.bean.dto.ProjectValueDto;
import cn.com.cifi.mars.bean.vo.workBench.ProjectIndexVO;
import cn.com.cifi.mars.constant.HrOrgTypeEunm;
import cn.com.cifi.mars.constant.IsDeleteEnum;
import cn.com.cifi.mars.constant.ModuleTypeEunm;
import cn.com.cifi.mars.constant.ProcessingStateEnum;
import cn.com.cifi.mars.constant.VersionStatusEnum;
import cn.com.cifi.mars.entity.MdmProject;
import cn.com.cifi.mars.entity.PriceVersion;
import cn.com.cifi.mars.entity.common.ProjectByStages;
import cn.com.cifi.mars.mapper.mars.PriceVersionMapper;
import cn.com.cifi.mars.mapper.mars.ProjectByStagesMapper;
import cn.com.cifi.mars.mapper.mars.ProjectConsoleMapper;
import cn.com.cifi.mars.mapper.mars.ProjectMapper;
import cn.com.cifi.mars.service.ProjectConsoleService;
import cn.com.cifi.mars.util.DateUtils;

@Service
public class ProjectConsoleServiceImpl implements ProjectConsoleService{
	
	@Autowired
	private ProjectByStagesMapper projectByStagesMapper;
	
	@Autowired
	private PriceVersionMapper priceVersionMapper;
	
	@Autowired
	private ProjectConsoleMapper projectConsoleMapper;

	@Autowired
	private ProjectMapper projectMapper;
	
	@Override
	public List<ProjectStagesDto> queryProjectStageList(List<MdmProject> projectIds) {
		return projectByStagesMapper.selectProjectStageList(projectIds,HrOrgTypeEunm.ORG_32.getKey());
	}

	@Override
	public List<PriceVersion> selectAllProject() {
		return priceVersionMapper.selectAllProject(IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<ProjectStagesVersionDto> selectVersionDataList(List<MdmProject> projectList) {
		return projectConsoleMapper.selectVersionDataList(projectList,ProcessingStateEnum.UNTREATED.getKey(),ModuleTypeEunm.PRICE.getKey(),IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<MdmProject> selectProjectByDataPermission(List<String> projectIds, String currentPage, String pageSize,
			String projectName) {
		Page<MdmProject>  pageDto = new Page<>();
		//参数当前页
		pageDto.setCurrent(Integer.parseInt(currentPage));
		//参数每页数量
		pageDto.setSize(Integer.parseInt(pageSize));
		return projectConsoleMapper.selectProjectByDataPermission(pageDto,projectIds,projectName,IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<ProjectUnderwayDto> selectUnderwayList(String projectId) {
		return projectConsoleMapper.selectUnderwayList(projectId,Arrays.asList(VersionStatusEnum.SUCCESSAPPROVED.getKey(), VersionStatusEnum.SYSGENERATION.getKey()),IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<ProjectByStages> selectParentData(String projectId) {
		return projectMapper.selectParentData(projectId);
	}

	@Override
	public List<ProjectStagesVersionDto> selectVersionDataListByStageId(String stageId) {
		return projectConsoleMapper.selectVersionDataListByStageId(stageId,IsDeleteEnum.NO.getKey());
	}

	@Override
	public List<ProjectCountDto> selectProjectCountList(List<String> projectIds, String date) {
		return projectConsoleMapper.selectProjectCountList(projectIds,date);
	}

	@Override
	public AllProjectIndexDto selectAllProjectIndex(List<String> projectIds,String date) {
		return projectConsoleMapper.selectAllProjectIndex(projectIds,date);
	}

	@Override
	public List<ProjectValueDto> selectProjectValueList(List<MdmProject> projectList, String date) {
		String  lastDate = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR));
		List<ProjectValueDto> list = projectConsoleMapper.selectProjectValueList(projectList,date);
		if(CollectionUtils.isEmpty(list)) {
			 list = projectConsoleMapper.selectProjectValueList(projectList,lastDate);
		}
		return list;
	}

	@Override
	public ProjectIndexVO selectProjectIndex(String projectId, String date) {
		return projectConsoleMapper.selectProjectIndex(projectId,date);
	}

	@Override
	public int selectTotalCount(List<String> projectIds, String projectName) {
		return projectConsoleMapper.selectTotalCount(projectIds,projectName,IsDeleteEnum.NO.getKey());
	}
}
